﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreGallery.Core.Data;
using CoreGallery.Addins.Contracts;
using System.Configuration;

namespace CoreGallery.Core.PhotoMgr
{
    /// <summary>
    /// Core storage abstraction for all photos housed in CoreGallery
    /// </summary>
    public class PhotoManager
    {
        private CoreGalleryIndex db = null;
        List<Core.Data.CoreObjects> _knownPhotos;

        public event EventHandler PhotoAddedEvent;
        public event EventHandler PhotoRemovedEvent;
        public event EventHandler PhotoUpdatedEvent;

        //public event EventHandler PhotoSelectedEvent;

        public event EventHandler GalleryAddedEvent;
        public event EventHandler GalleryRemovedEvent;
        public event EventHandler GalleryUpdatedEvent;

        public event EventHandler AlbumAddedEvent;
        public event EventHandler AlbumRemovedEvent;
        public event EventHandler AlbumUpdatedEvent;

        public PhotoManager()
        {
            string sdfPath = ConfigurationManager.ConnectionStrings["CoreGallery.Core.Properties.Settings.CoreGalleryIndexConnectionString"].ConnectionString;

            db = new CoreGalleryIndex(sdfPath);

            if (System.Diagnostics.Debugger.IsAttached) db.Log = Console.Out;

        }

        private void RaiseEvent(EventHandler h, object o)
        {
            if (h != null) h.DynamicInvoke(this, o);
        }

        /// <summary>
        /// Called by a given provider to add photo info to the storage engine
        /// </summary>
        /// <param name="newphoto"></param>
        public void AddPhoto(IPhoto newphoto)
        {
            // Extract metadata and thumbnail to store
            RaiseEvent(PhotoAddedEvent, newphoto);
        }

        public void RemovePhoto(IPhoto photo)
        {
            // Use UniqueId as key and remove references
            RaiseEvent(PhotoRemovedEvent, photo);
        }

        public void UpdatePhoto(IPhoto updatedPhoto)
        {
            // Use UniqueId as key and update metadata
            RaiseEvent(PhotoUpdatedEvent, updatedPhoto);
        }

        public IPhoto RetrievePhoto(Guid uniqueKey)
        {
            // TODO: 
            return null;
        }

        public void CreateGallery(IGallery newGallery)
        {
            // Create gallery, persist to index
            RaiseEvent(GalleryAddedEvent, newGallery);
        }

        public void CreateAlbum(IAlbum newAlbum)
        {
            // Create album, persist to index
            RaiseEvent(AlbumAddedEvent, newAlbum);
        }

        /// <summary>
        /// Perform a search.  Need to decide how criteria is passed in, how results are paged (yield?), etc.
        /// </summary>
        /// <returns></returns>
        public IList<IPhoto> Search()
        {
            return null;
        }

        public IList<IGallery> Galleries
        {
            get { return null; }
        }

        public IList<IAlbum> Albums
        {
            get { return null; }
        }

        /// <summary>
        /// Collection of all known keywords
        /// </summary>
        public IList<string> Keywords
        {
            get { return null; }
        }
    }
}
